package cn.tedu.yxcar.product.controller;

import cn.tedu.yxcar.product.pojo.dto.ArticleAddDTO;
import cn.tedu.yxcar.product.pojo.dto.ArticleUpdateDTO;
import cn.tedu.yxcar.product.pojo.vo.ArticleDetailsVO;
import cn.tedu.yxcar.product.pojo.vo.ArticleListVO;
import cn.tedu.yxcar.product.pojo.vo.ArticleSearchVO;
import cn.tedu.yxcar.product.pojo.vo.AuthorInfoVO;
import cn.tedu.yxcar.product.service.IArticleService;
import cn.tedu.yxcar.product.web.JsonPage;
import cn.tedu.yxcar.product.web.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章管理模块")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @PostMapping("/insert")
    @ApiOperation("添加文章")
    public JsonResult insert(@Valid ArticleAddDTO articleAddDTO) {
        articleService.insert(articleAddDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除文章")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "删除文章失败，ID值无效！")
                             @PathVariable Long id) {
        articleService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改文章详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "修改文章详情失败，ID值无效！")
                                     @PathVariable Long id, @Valid ArticleUpdateDTO articleUpdateDTO) {
        articleService.updateInfoById(id, articleUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/details")
    @ApiOperation("根据ID查询文章详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取文章详情失败，ID值无效！")
                                              Long id) {
        articleService.updateViewCountById(id);
        ArticleDetailsVO queryResult = articleService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/author")
    @ApiOperation("根据ID查询作者信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult getAuthorInfoByUserId(@Range(min = 1, message = "获取作者信息失败，ID值无效！")
                                                    Long userId) {
        AuthorInfoVO queryResult = articleService.getAuthorInfoByUserId(userId);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/list")
    @ApiOperation("查询文章列表")
    public JsonResult list() {
        List<ArticleListVO> list = articleService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/search")
    @ApiOperation("文章搜索模块")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "page", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10"),
            @ApiImplicitParam(value = "关键字", name = "keywords", example = "春季")
    })
    public JsonResult<JsonPage<ArticleSearchVO>> selectByKeyword(
            @Param("page") Integer page, @Param("pageSize") Integer pageSize, @Param("keywords") String keywords) {
        PageHelper.startPage(page,pageSize);
        List<ArticleSearchVO> list = articleService.listSearchByKeywords(keywords);
        JsonPage<ArticleSearchVO> jsonPage = JsonPage.restPage(PageInfo.of(list));
        return JsonResult.ok(jsonPage);
    }
}
